/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.epam.expertsys.dbconn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.IOException;
import org.h2.jdbcx.JdbcConnectionPool;
import java.util.ArrayList;
import com.epam.expertsys.blogic.*;

/**
 *
 * @author Admin
 */
public class PatientDAOH2 extends GenericDAOH2 implements PatientDAOIface {

    public PatientDAOH2(int maxConnCount) throws SQLException {
        super(maxConnCount);
        super.start();
    }

    @Override
    public Patient getPatientByLogin(String login) throws SQLException {
        Connection conn = null;
        PreparedStatement prepStat = null;
        Patient getPatient = null;

        try {
            conn = conPool.getConnection();
            prepStat = conn.prepareStatement("Select * from patient where (login = ?)");

            prepStat.setString(1, login);

            ResultSet resultSet = prepStat.executeQuery();

            //if (resultSet.)
            resultSet.next();

            Long patient_id = resultSet.getLong("PATIENT_ID");
            if (resultSet.wasNull()) {
                patient_id = null;
            }


            String name = resultSet.getString("NAME");
            if (resultSet.wasNull()) {
                name = null;
            }

            String surname = resultSet.getString("Surname");
            if (resultSet.wasNull()) {
                surname = null;
            }

            Long insurance = resultSet.getLong("insurance_no");
            if (resultSet.wasNull()) {
                insurance = null;
            }

            String password = resultSet.getString("password");
            if (resultSet.wasNull()) {
                password = null;
            }

            getPatient = new Patient(patient_id, name, surname, insurance,
                    login, password);

        } catch (SQLException e) {
            throw new SQLException("SQL problems in PatientDAOH2", e);
        } finally {
            closeStatement(prepStat);
            closeConnection(conn);
        }

        return getPatient;
    }
}
